#include "application.h"
#include <qDebug.h>

Application::Application ( int & argc, char ** argv )
    : QApplication(argc, argv)
{
    m_db = this->openDB();
    if(!m_db.isOpen())
    {
        QMessageBox::information(0, "Error", QString("(!m_db.isOpen())"));
    }
}

QSqlDatabase Application::openDB()
{
#if 0x1
    QString v_db_path = "C:/yt_dump.mdb";
    QSqlDatabase v_db = QSqlDatabase::addDatabase("QODBC");
    v_db.setDatabaseName(
            QString("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=%1")
            .arg(v_db_path)
            );
#else
    QString v_db_path = "E:/Qt/yt_dump.sqlite";
    QSqlDatabase v_db = QSqlDatabase::addDatabase("QSQLITE");
    v_db.setDatabaseName(v_db_path);
#endif
    v_db.open();
    return v_db;
}

QString Application::generateTestHtml()
{
    QSqlQuery v_query;
    //v_query.prepare("select * from fv_akmiyoshi");
    v_query.prepare("select * from fv_akmiyoshi order by pos asc");
    if(!v_query.exec())
    {
        return "(!v_query.exec())";
    }


#if 0x0
    QFile v_file_1(":/jslib/jquery-1.8.2.min.js");
    v_file_1.open(QIODevice::ReadOnly);
    QString v_script_1 = v_file_1.readAll();
    v_file_1.close();
    qDebug() << "[v_script_1]" << v_script_1;
#endif


    QString v_htmlText;

    v_htmlText += QString("<html>\n");

    v_htmlText += QString("<head>\n");
    // Lazy Load Plugin for jQuery : http://www.appelsiini.net/projects/lazyload
    v_htmlText += "  <script type='text/javascript' src='qrc:/jslib/jquery-1.8.2.min.js' charset='UTF-8'></script>\n";
    //v_htmlText += "  <script type='text/javascript' src='qrc:/jslib/jquery.lazyload-1.8.1.js' charset='UTF-8'></script>\n";
    v_htmlText += "  <script type='text/javascript' src='qrc:/jslib/jquery.lazyload-1.8.4.js' charset='UTF-8'></script>\n";

    v_htmlText += QString("<style>\n");
    v_htmlText += QString("#div001{\n");
    v_htmlText += QString("  width:700px;\n");
    v_htmlText += QString("  height:500px;\n");
    v_htmlText += QString("  overflow-x:hidden;\n");
    v_htmlText += QString("  overflow-y:scroll;\n");
    v_htmlText += QString("  border:1px solid blue;\n");
    v_htmlText += QString("}\n");
    v_htmlText += QString("</style>\n");
    v_htmlText += QString("</head>\n");
    v_htmlText += QString("<body>\n");

    v_htmlText += QString("<div id='div001'>\n");

    while(v_query.next())
    {
        QString v_id = v_query.value( v_query.record().indexOf("id") ).toString();
        v_htmlText += "<table border=1><tr><td>";
        v_htmlText += QString("<image width=120 height=90 src='qrc:/img/grey.gif' data-original='http://i.ytimg.com/vi/%1/2.jpg' onclick='web1.submitIdToPlay(\"%1\")' />")
        //v_htmlText += QString("<image width=120 height=90 src='qrc:/img/loading.gif' data-original='http://i.ytimg.com/vi/%1/2.jpg' onclick='web1.submitIdToPlay(\"%1\")' />")
                .arg(v_id);
        v_htmlText += "</td><td style='width:500;'>";
        v_htmlText += QString("<button onclick='web1.submitIdToDownload(\"%1\")'>ダウンロード</button>\n").arg(v_id);
        v_htmlText += "<br />\n";
        v_htmlText += v_query.value( v_query.record().indexOf("title") ).toString();
        v_htmlText += "<br />\n";
        v_htmlText += QString("[%1]").arg(v_id);
        v_htmlText += "<br />\n";
        v_htmlText += "</td></tr></table>";
        //v_htmlText += "</div>";
    }

    v_htmlText += QString("</div>\n");

    v_htmlText += "  <script type='text/javascript'>\n";
    v_htmlText += QString("    $('img').lazyload({ container:$('#div001'), effect:'fadeIn', threshold:0 });\n");

    v_htmlText += "  </script>\n";

    v_htmlText += QString("</body>\n");
    v_htmlText += QString("</html>\n");
    qDebug() << "[v_htmlText]" << v_htmlText;
    return v_htmlText;
}
